﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRProject.Code.DTO;
using System.Collections.Generic;
using HRProject.Code.Common;

namespace HRProject.Code.DAO
{
    public class AssetDAO : BaseDAO
    {
        public AssetDAO()
            : base ()
        {
        }

        public List<BLAsset> search(BLAsset filter)
        {
            List<BLAsset> result = new List<BLAsset>();
            List<BaseCondition> condition = new List<BaseCondition>();
            condition.Add(new BaseCondition("@id", filter.Id,ParameterDirection.Input));
            condition.Add(new BaseCondition("@name", filter.Name,ParameterDirection.Input));
            condition.Add(new BaseCondition("@typeId", filter.Type.Id, ParameterDirection.Input));
            DataTable data = select("usp_searchAssets", condition);
            for (int i = 0; i < data.Rows.Count; i++)
            {
                BLAsset asset = new BLAsset();
                asset.Id = (Guid)data.Rows[i]["AssetId"];
                asset.Name = (string)data.Rows[i]["AssetName"];
                asset.Description = (string)data.Rows[i]["Description"];
                asset.ProductId = (string)data.Rows[i]["ProductId"];
                asset.ImageUrl = (string)data.Rows[i]["ImageUrl"];
                asset.ImportDate = (DateTime)data.Rows[i]["ImportDate"];
                asset.TerminateDate = (DateTime)data.Rows[i]["TerminateDate"];
                asset.Type = new BLAssetType();
                asset.Type.Id = (Guid)data.Rows[i]["AssetTypeId"];
                asset.Type.Name = (string)data.Rows[i]["AssetTypeName"];
                asset.DataSignature = (long)data.Rows[i]["DataSignature"];
                asset.CreateDate = (DateTime)data.Rows[i]["CreateDate"];
                asset.CreateUserId = (Guid)data.Rows[i]["CreateUserId"];
                asset.UpdateDate = (DateTime)data.Rows[i]["UpdateDate"];
                asset.UpdateUserId = (Guid)data.Rows[i]["UpdateUserId"];
                result.Add(asset);
            }
            return result;
        }

        public int insert(BLAsset asset)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            int errorCode = 0;
            condition.Add(new BaseCondition("@id", Guid.NewGuid(), ParameterDirection.Input));
            condition.Add(new BaseCondition("@name",asset.Name,ParameterDirection.Input));
            condition.Add(new BaseCondition("@typeId", asset.Type.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@productId", asset.ProductId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@imageUrl", asset.ImageUrl, ParameterDirection.Input));
            if (asset.ImportDate == null)
                asset.ImportDate = DateTime.Now;
            condition.Add(new BaseCondition("@importDate", asset.ImportDate, ParameterDirection.Input));
            if (asset.TerminateDate == null)
                asset.TerminateDate = Constant.DefaultMaxDate;
            condition.Add(new BaseCondition("@terminateDate", asset.TerminateDate, ParameterDirection.Input));
            condition.Add(new BaseCondition("@description", asset.Description, ParameterDirection.Input));
            condition.Add(new BaseCondition("@createUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature",asset.DataSignature,ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode",errorCode,ParameterDirection.Output));
            int result = update("usp_addAsset", condition);
            return result;
        }

        public int update(BLAsset asset)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            int errorCode = 0;
            condition.Add(new BaseCondition("@id", asset.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@name", asset.Name, ParameterDirection.Input));
            condition.Add(new BaseCondition("@typeId", asset.Type.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@productId", asset.ProductId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@imageUrl", asset.ImageUrl, ParameterDirection.Input));
            if (asset.ImportDate == null)
                asset.ImportDate = DateTime.Now;
            condition.Add(new BaseCondition("@importDate", asset.ImportDate, ParameterDirection.Input));
            if (asset.TerminateDate == null)
                asset.TerminateDate = Constant.DefaultMaxDate;
            condition.Add(new BaseCondition("@terminateDate", asset.TerminateDate, ParameterDirection.Input));
            condition.Add(new BaseCondition("@description", asset.Description, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", asset.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_updateAsset", condition);
            return result;
        }

        public int Delete(BLAsset asset, ref int errorCode)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            errorCode = 0;
            condition.Add(new BaseCondition("@id", asset.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", asset.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_deleteAsset", condition);
            errorCode = (int)condition[condition.Count - 1].ParamValue;
            return result;
        }

        public List<BLAsset> Search(BLAsset filter, string sortColumn, bool sortDirection, int totalPage, int pageSize, int pageIndex)
        {
            int startIndex = (pageIndex - 1) * pageSize + 1;
            if (pageIndex > totalPage)
            {
                startIndex = (totalPage - 1) * pageSize + 1;
            }
            else if (pageIndex < 1)
            {
                startIndex = 1;
            }
            int endIndex = startIndex + pageSize - 1;

            string sort = sortColumn + (sortDirection ? "Asc" : "Desc");

            List<BLAsset> result = new List<BLAsset>();
            List<BaseCondition> condition = new List<BaseCondition>();
            condition.Add(new BaseCondition("@id", filter.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@name", filter.Name, ParameterDirection.Input));
            condition.Add(new BaseCondition("@typeId", filter.Type.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@importDownDate", filter.ImportDate, ParameterDirection.Input));
            condition.Add(new BaseCondition("@importUpDate", filter.TerminateDate, ParameterDirection.Input));
            condition.Add(new BaseCondition("@sort", sort, ParameterDirection.Input));
            condition.Add(new BaseCondition("@startIndex", startIndex, ParameterDirection.Input));
            condition.Add(new BaseCondition("@endIndex", endIndex, ParameterDirection.Input));
            DataTable data = select("usp_SearchAssetsInTypes", condition);
            for (int i = 0; i < data.Rows.Count; i++)
            {
                BLAsset asset = new BLAsset();
                asset.Id = (Guid)data.Rows[i]["AssetId"];
                asset.Name = (string)data.Rows[i]["AssetName"];
                asset.ImportDate = (DateTime)data.Rows[i]["ImportDate"];
                asset.TerminateDate = (DateTime)data.Rows[i]["TerminateDate"];
                asset.Description = (string)data.Rows[i]["Description"];
                asset.Type = new BLAssetType();
                asset.Type.Name = (string)data.Rows[i]["AssetTypeName"];
                result.Add(asset);
            }
            return result;
        }

        public int QuickAdd(BLAsset asset, ref int errorCode)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            errorCode = 0;
            condition.Add(new BaseCondition("@id", asset.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@name", asset.Name, ParameterDirection.Input));
            condition.Add(new BaseCondition("@typeId", asset.Type.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@description", asset.Description, ParameterDirection.Input));
            condition.Add(new BaseCondition("@importDate", asset.ImportDate, ParameterDirection.Input));
            condition.Add(new BaseCondition("@terminateDate", asset.TerminateDate, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", asset.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_QuickAddAsset", condition);
            errorCode = (int)condition[condition.Count - 1].ParamValue;
            return result;
        }

        public int QuickUpdate(BLAsset asset, ref int errorCode)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            errorCode = 0;
            condition.Add(new BaseCondition("@id", asset.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@name", asset.Name, ParameterDirection.Input));
            condition.Add(new BaseCondition("@description", asset.Description, ParameterDirection.Input));
            condition.Add(new BaseCondition("@importDate", asset.ImportDate, ParameterDirection.Input));
            condition.Add(new BaseCondition("@terminateDate", asset.TerminateDate, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", asset.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_QuickUpdateAsset", condition);
            errorCode = (int)condition[condition.Count - 1].ParamValue;
            return result;
        }
    }
}
